package ko;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

import ko.util.IHitTypes;

public class KCircleEntity extends KEntity implements IHitTypes{//IHitTypes
	private float parame1 = 0.0001f;
	private float _radius = 0;
	private float _diameter;

	public KCircleEntity(float x, float y,float radius) {			
		super(x, y);
		
		this.setRadius(radius);
		
		this.hitLayers = new ArrayList<String>();
		this.mx = -_radius;
		this.my = -_radius;
	}
	
	public float getRadius() {
		return _radius;
	}		
	public void setRadius(float value) {
		_radius = value;
		_diameter = _radius * 2;
	}
	public float getDiameter() {
		return _diameter;
	}
	
	@Override 
	public Boolean containsMouse()
	{
		float ex = this.x + this.mx;
		float ey = this.y + this.my;
		return (ex <= KInput.mouseX() && ey <= KInput.mouseY()
		&& this.getRadius() + ex > KInput.mouseX()
		&& this.getRadius() + ey > KInput.mouseY());
	}
	@Override 
	public void draw(int color)
	{
		this.renderData = Bitmap.createBitmap(Math.round(this.getDiameter()),Math.round(this.getDiameter()), Config.ARGB_8888);
		Canvas canvasTemp = new Canvas(renderData);
		Paint paint = new Paint();
		paint.setColor(color);
		canvasTemp.drawCircle(this.getRadius(), this.getRadius(), this.getRadius(), paint);
	}

	public void vsOBB(KRectangleEntity rect) {
		Boolean boolx = false, booly = false;
		this.x -= vx;
		if (this.vy != 0 && KHit.RectHitCircle(rect, this) == true) {
			if (this.x >= rect.x && this.x <= rect.x + rect.width) {
				this.y = (this.vy > 0)
					? rect.y - this.getRadius() - parame1
					: rect.y + rect.height + this.getRadius() + parame1;
			} else {
				booly = true;
				this.y -= this.vy;
			}
		}
		this.x += vx;
		
		this.y -= vy;
		if (this.vx != 0 && KHit.RectHitCircle(rect, this) == true) {
			if (this.y >= rect.y && this.y <= rect.y + rect.height) {
				this.x = (this.vx > 0)
				? rect.x - this.getRadius() - parame1
				: rect.x + rect.width + this.getRadius() + parame1;
			} else {
				boolx = true;
				this.x -= this.vx;
			}
		}
		this.y += vy;
		
	}

	public void vsCircle(KCircleEntity circle) {
		if (KHit.CircleHitCircle(this, circle) == true) {
			float dx = this.x - circle.x
			,dy = this.y - circle.y
			,w2 = this.getRadius() + circle.getRadius();
			
			float angle1 = new Float(Math.atan2(dy, dx));
			float angle2 = new Float(Math.atan2(this.vy, this.vx));
			
			this.x = circle.x + new Float(Math.cos(angle1)) * w2;
			this.y = circle.y + new Float(Math.sin(angle1)) * w2;
			
			angle2 += (angle1 > angle2) ? -0.01 : 0.01;
			vx = new Float(Math.cos(angle2)) * speed;
			vy = new Float(Math.sin(angle2)) * speed;
			this.x += vx;
			this.y += vy;
		}
	}
}
